﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Kiosk
{
    public class Database
    {
        private static Database _instance;
        public static Database Get
        {
            get
            {
                if (_instance == null)
                    _instance = new Database();
                return _instance;
            }
        }
        Dictionary<string, Urun> Db;
        private Database()
        {
            Db = new Dictionary<string, Urun>();
        }

        public void Yukle()
        {
            if (File.Exists(Application.StartupPath + "\\URUN.DAT"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + "\\URUN.DAT", System.Text.Encoding.GetEncoding("ibm857")))
                {
                    string barkod;
                    string line;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine();
                        barkod = line.Substring(7, 20).Trim(); ;
                        if (Db.ContainsKey(barkod))
                        {
                            Db[barkod].UrunAdi = line.Substring(27, 20).Trim();
                            Db[barkod].Fiyati = Convert.ToDecimal(line.Substring(47, 10).Replace('.', ',').Trim());
                            Db[barkod].PluNo = Convert.ToInt32(line.Substring(1, 6));
                        }
                        else
                        {
                            Urun urn = new Urun();
                            urn.UrunAdi = line.Substring(27, 20).Trim();
                            urn.Fiyati = Convert.ToDecimal(line.Substring(47, 10).Replace('.', ',').Trim());
                            urn.Barkod = barkod;
                            urn.PluNo = Convert.ToInt32(line.Substring(1, 6));
                            Db.Add(barkod, urn);
                        }
                    }
                    sr.Close();
                }
            }


            List<Toptan> _top = new List<Toptan>();
            if (File.Exists(Application.StartupPath + "\\toptan.dat"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + "\\toptan.dat", System.Text.Encoding.GetEncoding("ibm857")))
                {
                    string[] line;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine().Split(',');
                        Toptan top = new Toptan();
                        top.PluNo = Convert.ToInt32(line[0]);
                        top.FiyatNo = Convert.ToInt32(line[1]);
                        top.Fiyat = Convert.ToDecimal(line[2].Replace('.', ','));
                        _top.Add(top);
                    }
                    sr.Close();
                }
            }

            //plu no ataması yapılıyor ve toptan ataması
            foreach (Urun urn in Db.Values)
            {
                Toptan[] tops = _top.Where(x => x.PluNo == urn.PluNo).ToArray();
                if (tops.Length > 0)
                {
                    urn.Fiyatlar.AddRange(tops);
                }
            }
        }

        public Urun Find(string barkod)
        {
            if (Db.ContainsKey(barkod))
            {
                return Db[barkod];
            }
            else
            {
                return null;
            }
        }
    }

    public class Urun
    {
        List<Toptan> _fiyatlar = new List<Toptan>();
        public string Barkod { get; set; }
        public string UrunAdi { get; set; }
        public decimal Fiyati { get; set; }
        public decimal KartFiyati { get; set; }
        public int PluNo { get; set; }
        public List<Toptan> Fiyatlar { get { return _fiyatlar; } }
    }

    public class Toptan
    {
        public int PluNo { get; set; }
        public int FiyatNo { get; set; }
        public decimal Fiyat { get; set; }
    }

    public class Plu
    {
        public int PluNo { get; set; }
        public int Satir { get; set; }
    }
}
